from base import BaseHandler
from google.appengine.api.labs.taskqueue import taskqueue
from google.appengine.ext import db
import logging

class CleanerStarter(BaseHandler):
	def get(self):
		taskqueue.add(url='/cleaner/run', params = {
			'entity': 'UserPrefs'
		})
		taskqueue.add(url='/cleaner/run', params = {
			'entity': 'Message'
		})
		taskqueue.add(url='/cleaner/run', params = {
			'entity': 'Blockerator'
		})
		self.response.out.write('ok')
	
class CleanerRunner(BaseHandler):
	COUNT = 100
	
	def post(self):
		entityName = self.request.get('entity')
		query = db.GqlQuery("SELECT __key__ FROM %s" % entityName)
		array = query.fetch(CleanerRunner.COUNT, 0)
		
		logging.info('cleaning %d of %s' % (len(array), entityName))
		
		db.delete(array)
			
		if len(array) == CleanerRunner.COUNT:
			taskqueue.add(url='/cleaner/run', params = {
				'entity': entityName
			})
		